var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $GameEventType = require("GameEventType");
var $1$EventManager = require("EventManager");
var r = cc__importDefault($1$EventManager);
var $1$GameConfig = require("GameConfig");
var a = cc__importDefault($1$GameConfig);
var def_PowerManager = function () {
  function _ctor() { }
  _ctor.init = function () {
    this.data = this.loadOldData();
    this.startAutoRecoverPower();
    this.onEvents();
  };
  _ctor.loadOldData = function () {
    // 创建新的对象实例
    const instance = new c();

    // 从本地存储获取游戏的PowerData
    const storedPowerData = cc.sys.localStorage.getItem(`${a.default.gameName}PowerData`);

    if (storedPowerData) {
      const parsedData = JSON.parse(storedPowerData);
      instance.curPower = parsedData.curPower;
      instance.lastSaveTime = parsedData.lastSaveTime;
    }

    return instance;
  };
  _ctor.startAutoRecoverPower = function () {
    // 获取当前上下文
    const context = this;

    // 获取上次保存时间和当前时间
    const lastSaveTime = this.data.lastSaveTime;
    const currentTime = Date.now();
    const lastSaveDate = new Date(lastSaveTime).getDate();
    const currentDate = new Date(currentTime).getDate();

    if (currentDate > lastSaveDate) {
      // 如果当前日期大于上次保存日期
      this.data.lastSaveTime = currentTime;
      this.addPower(this.data.maxPower);
      setInterval(this.recoverPower.bind(this), this.data.recoverPowerInterval, this.data.recoverPowerValue);
    } else {
      // 计算时间差
      const timeDiff = currentTime - lastSaveTime;
      const recoveredPower = Math.floor(timeDiff / this.data.recoverPowerInterval) * this.data.recoverPowerValue;
      const remainingTime = timeDiff % this.data.recoverPowerInterval;

      if (recoveredPower > 0) {
        this.data.lastSaveTime = currentTime - remainingTime;
        this.addPower(recoveredPower);
      }

      // 延迟设置间隔恢复
      setTimeout(() => {
        setInterval(context.recoverPower.bind(context), context.data.recoverPowerInterval, context.data.recoverPowerValue);
      }, remainingTime);
    }
  };
  _ctor.recoverPower = function () {
    this.data.addPower(this.data.recoverPowerValue);
    this.data.updateLastSaveTime();
    this.saveData();
    this.emit($GameEventType.EventType.AssetEvent.powerChanged, this.data.curPower);
  };
  _ctor.onEvents = function () {
    r.default.on($GameEventType.EventType.AssetEvent.consumePower, this.onConsumePower, this);
    r.default.on($GameEventType.EventType.AssetEvent.getPower, this.addPower, this);
  };
  _ctor.getData = function () {
    return {
      maxPower: this.data.maxPower,
      curPower: this.data.curPower,
      totalTime: Math.floor(.001 * this.data.recoverPowerInterval),
      curTime: Math.floor(.001 * (Date.now() - this.data.lastSaveTime))
    };
  };
  _ctor.saveData = function () {
    cc.sys.localStorage.setItem(a.default.gameName + "powerData", JSON.stringify(this.data));
  };
  _ctor.addPower = function (t) {
    // 如果 t 未定义，则将其设为 1
    if (typeof t === 'undefined') {
      t = 1;
    }

    // 增加能量
    this.data.addPower(t);

    // 保存数据
    this.saveData();

    // 触发能量变化事件
    this.emit($GameEventType.EventType.AssetEvent.powerChanged, this.data.curPower);
  };
  _ctor.subPower = function (t) {
    if (this.data.subPower(t)) {
      this.saveData();
      this.emit($GameEventType.EventType.AssetEvent.powerChanged, this.data.curPower);
    }
  };
  _ctor.onConsumePower = function (t) {
    // 获取 power 值
    let powerAmount = t.power;

    // 如果 t.power 为空，则设为 1
    if (powerAmount === null) {
      powerAmount = 1;
    }

    // 尝试减少能量
    if (this.data.subPower(powerAmount)) {
      // 如果能量减少成功，调用回调
      if (t.cb) {
        t.cb();
      }
    } else {
      // 如果能量不足，提示用户
      this.tipPowerUnEnough();
    }
  };
  _ctor.tipPowerUnEnough = function () {
    this.emit($GameEventType.EventType.AssetEvent.powerUnEnough, this.data.curPower);
  };
  _ctor.emit = function (t, e) {
    r.default.emit(t, e);
  };
  _ctor.data = null;
  return _ctor;
}();
exports.default = def_PowerManager;
var c = function () {
  function t() {
    this.maxPower = 10;
    this.recoverPowerInterval = 3e5;
    this.recoverPowerValue = 1;
    this.curPower = 10;
    this.lastSaveTime = 0;
    this.curPower = 10;
    this.lastSaveTime = Date.now();
  }
  t.prototype.addPower = function (t) {
    undefined === t && (t = 1);
    this.curPower += t;
    this.curPower > this.maxPower && (this.curPower = this.maxPower);
  };
  t.prototype.subPower = function (t) {
    return !(this.curPower < t || (this.curPower -= t, 0));
  };
  t.prototype.updateLastSaveTime = function () {
    this.lastSaveTime = Date.now();
  };
  return t;
}();